package com.gpnu.crm.shiro;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Component;

import com.gpnu.crm.util.JSONResult;
import com.gpnu.crm.util.JSONUtil;

@Component("formFilter")
public class CRMFormFilter  extends FormAuthenticationFilter{
	  protected boolean onLoginSuccess(AuthenticationToken token, Subject subject, ServletRequest request, ServletResponse response) throws Exception {
		    response.setContentType("text/json;charset=UTF-8");
		    response.getWriter().println(JSONUtil.toJSONString(new JSONResult()));
		    return false;
		}

		  protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
		    e.printStackTrace();
		    try {
		      JSONResult result = new JSONResult();
		      result.mark("账号和密码不匹配");
		      response.setContentType("text/json;charset=UTF-8");
		      response.getWriter().println(JSONUtil.toJSONString(result));
		    } catch (Exception e1) {
		      throw new RuntimeException(e1);
		    } 
		    return false;
		  }

	

}
